package com.haoshuang.zhouzhoubang.ui.dialog

import android.content.Context
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.DialogChooseDateRangeBinding
import com.lxj.xpopup.core.BottomPopupView
import com.mufeng.libs.utils.ktx.formatTen
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.visibleOrGone

class ChooseDateRangeDialog(
    context: Context,
    private val isEndToday: Boolean = true,
    private val submit: (start_date: String, end_date: String) -> Unit
) : BottomPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_choose_date_range
    }

    private lateinit var binding: DialogChooseDateRangeBinding
    private var isStart = true

    private var start_date = ""
    private var end_date = ""

    override fun onCreate() {
        super.onCreate()
        binding = DialogChooseDateRangeBinding.bind(popupImplView)



        binding.tvChooseStartDate.click {
            isStart = true
            initDateView()
        }

        binding.tvChooseEndDate.click {
            if (start_date.isEmpty()) {
                toast("请先选择开始时间")
                return@click
            }
            isStart = false
            initDateView()
            val year = start_date.split("-")[0].toInt()
            val month = start_date.split("-")[1].toInt()
            val day = start_date.split("-")[2].toInt()
            binding.dateWheelLayoutEnd.setRange(
                DateEntity.target(year, month, day),
                if (isEndToday) DateEntity.today() else null,
                DateEntity.target(year, month, day)
            )
            end_date =
                "${binding.dateWheelLayoutEnd.selectedYear}-${binding.dateWheelLayoutEnd.selectedMonth.formatTen()}-${binding.dateWheelLayoutEnd.selectedDay.formatTen()}"
            binding.tvChooseEndDate.text = end_date
            binding.dateWheelLayoutEnd.setOnDateSelectedListener { year, month, day ->
                end_date = "$year-${month.formatTen()}-${day.formatTen()}"
                binding.tvChooseEndDate.text = end_date
            }
        }

        initDateView()

        binding.dateWheelLayoutStart.setRange(
            DateEntity.target(2023, 1, 1),
            DateEntity.today(),
            DateEntity.today()
        )
        start_date =
            "${binding.dateWheelLayoutStart.selectedYear}-${binding.dateWheelLayoutStart.selectedMonth.formatTen()}-${binding.dateWheelLayoutStart.selectedDay.formatTen()}"
        binding.tvChooseStartDate.text = start_date
        binding.dateWheelLayoutStart.setOnDateSelectedListener { year, month, day ->
            start_date = "$year-${month.formatTen()}-${day.formatTen()}"
            binding.tvChooseStartDate.text = start_date
        }

        binding.tvSubmit.clickWithTrigger {
            if (start_date.isEmpty()) {
                toast("请选择开始时间")
                return@clickWithTrigger
            }
            if (end_date.isEmpty()) {
                toast("请选择结束时间")
                return@clickWithTrigger
            }
            dismiss()
            submit(start_date, end_date)
        }

    }

    private fun initDateView() {
        binding.tvChooseStartDate.isSelected = isStart
        binding.tvChooseEndDate.isSelected = !isStart

        binding.dateWheelLayoutStart.visibleOrGone(isStart)
        binding.dateWheelLayoutEnd.visibleOrGone(!isStart)
    }

}